//TODO
YupooStream.prototype.init = function() {
    var v = nsPreferences.getBoolPref("cn.xpies.yupoostream.status");
    if (v) {
        
    } else {
        document.getElementById("yupoostream-status-image").setAttribute("style","list-style-image: url('chrome://yupoostream/skin/yupoostream16_q.png')");
    }
    
	window.addEventListener( "DOMContentLoaded", ypstream.onPageLoaded, false );
	window.addEventListener( "pageshow", ypstream.onPageLoaded, false );
}
YupooStream.prototype.changeStatus = function(status) {
	if (status) {
		ypstream.onPageLoaded();
	}
}
YupooStream.prototype.onClickStatus = function(e) {
	if (e.button == 0) {
		var v = nsPreferences.getBoolPref("cn.xpies.yupoostream.status");
		v = !v;
        if (v) {
            document.getElementById("yupoostream-status-image").setAttribute("style","list-style-image: url('chrome://yupoostream/skin/yupoostream16.png')");
        } else {
            document.getElementById("yupoostream-status-image").setAttribute("style","list-style-image: url('chrome://yupoostream/skin/yupoostream16_q.png')");
        }
		nsPreferences.setBoolPref("cn.xpies.yupoostream.status", v);
		ypstream.changeStatus(v);
	}
}

YupooStream.prototype.onPageLoaded = function(e) {
    if (window._content.location.toString().indexOf("yupoo.com/photos/view")>0) {
		if (nsPreferences.getBoolPref("cn.xpies.yupoostream.status")) {
			var _window = window._content;
	        var pageHead = _window.document.getElementsByTagName("head")[0];
	    
	        if (pageHead == null) { // if page head doesn't exist, create one
	          var pageHTML = _window.document.getElementsByTagName("html")[0];
	          var pageHead = _window.document.createElement("head");
	    
	          pageHTML.appendChild(pageHead);
	    
	          var pageHead = _window.document.getElementsByTagName("head")[0];
	        }
	    
	        var jsCheck = _window.document.getElementById("yupoostream_js");
	    
	        if (jsCheck == null) { // insert javascript reference
	          var js = _window.document.createElement("script");
	          js.setAttribute("id", "yupoostream_js");
	          js.setAttribute("language", "JavaScript");
	          js.setAttribute("type", "text/javascript");
	//          js.setAttribute("src", "http://www.douban.com/js/douban10194.js");
	          js.setAttribute("src", "chrome://yupoostream/content/yupoostreamex.js");
	    
	          pageHead.appendChild(js);
	        }
	        var jsReflex = _window.document.getElementById("yupoostream_reflex");
	    
	        if (jsCheck == null) { // insert javascript reference
	          var js = _window.document.createElement("script");
	          js.setAttribute("id", "yupoostream_reflex");
	          js.setAttribute("language", "JavaScript");
	          js.setAttribute("type", "text/javascript");
	//          js.setAttribute("src", "http://www.douban.com/js/douban10194.js");
	          js.setAttribute("src", "chrome://yupoostream/content/cvi_reflex_lib.js");
	    
	          pageHead.appendChild(js);
	        }
	        var cssCheck = _window.document.getElementById("yupoostream_pie_css");
	    
	        if (cssCheck == null) { // insert stylesheet reference
	          var css = _window.document.createElement("link");
	          css.setAttribute("id", "yupoostream_pie_css");
	          css.setAttribute("rel", "stylesheet");
	          css.setAttribute("type", "text/css");
	          css.setAttribute("href", "chrome://yupoostream/skin/yupoostream_pie.css");
	    
	          pageHead.appendChild(css);
	        }
	        
	        var playbutton = _window.document.getElementById("yupoostream_playbutton");
	        if (playbutton == null) {
	        	playbutton = _window.document.createElement("div");
	        	playbutton.setAttribute("id","yupoostream_playbutton");
	        	playbutton.setAttribute("title","点击播放");
	        	playbutton.setAttribute("class","yupoostream_playbutton");
	        	playbutton.setAttribute("style","background:url('chrome://yupoostream/skin/play_small.png') no-repeat right;" +
	                    "height:17px; position:absolute; right:0px; text-align:left; top:3pt; width:21px;");
	        	playbutton.setAttribute("onclick", "init();");
	            if (_window.document.getElementById("ctx_stream_head")) {
	        	   _window.document.getElementById("ctx_stream_head").appendChild(playbutton);
	            }
	        }
	        
	//        _window.document.photoSize = nsPreferences.copyUnicharPref("cn.xpies.yupoostream.photosize","big");
		}
    }
}

YupooStream.prototype.getContextLinkURL = function() {
   return (gContextMenu ? gContextMenu.link.toString() : null);
}


YupooStream.prototype.destory = function() {
	window.removeEventListener( "DOMContentLoaded", ypstream.onPageLoaded, false);
	window.removeEventListener( "pageshow", ypstream.onPageLoaded, false);
	var win = window._content;
	var doc = win.document;
	var back = doc.getElementById("yupoostream_back");
	if (back != null) {
		doc.removeChild(back);
	}
}

var ypstream = new YupooStream();

window.addEventListener( 'load', ypstream.init, false);
window.addEventListener( 'unload', ypstream.destory, false);
